package com.itheima.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password !=null){
            //加密密码
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {//2018.05
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month+".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Member> rows = page.getResult();
        return new PageResult(total,rows);


    }

//    //分页查询会员的方法
//    @Override
//    public PageResult findPage(QueryPageBean queryPageBean) {
//        //获取需要的参数
//        Integer pageSize = queryPageBean.getPageSize();
//        Integer currentPage = queryPageBean.getCurrentPage();
//
//        String queryString = queryPageBean.getQueryString();
//        //基于分页助手插件实现 完成分页查询
//        PageHelper.startPage(currentPage,pageSize);
//
//        //调用dao层的方法进行 条件查询 查询到的是Member的集合
//        Page<Member> page = memberDao.selectByCondition(queryString);
//
//        long total = page.getTotal();//总的记录数
//        List<Member> rows = page.getResult();//查询的结果（查询到的Member的list集合）
//
//        return new PageResult(total,rows);
//    }
    //编辑会员
    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }
    //根据id查询会员，用于数据的回显
    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }
    //删除会员的方法
    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);

    }
}
